package com.dhcc.cdc.busi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDictypeEntity;
import com.dhcc.cdc.busi.service.ICdcDictypeService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 业务字典类型
 * 
 * @ClassName: CdcDictypeController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:09
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcdictype")
public class CdcDictypeController extends BaseController {

    private String PREFIX = "/backend/cdc/cdcdictype/";

    @Autowired
    private ICdcDictypeService cdcDictypeService;

    /**
     * 跳转到业务字典类型列表首页
     */
    @RequestMapping("")
    @Permission("cdcdictype:list")
    public String index() {
        return PREFIX + "cdcdictype.html";
    }

    /**
     * 跳转到新增业务字典类型
     */
    @RequestMapping("/to_add")
    @Permission("cdcdictype:add")
    public String toAdd() {
        return PREFIX + "cdcdictype_add.html";
    }

    /**
     * 跳转到修改业务字典类型
     */
    @RequestMapping("/to_update/{cdcDictypeId}")
    @Permission("cdcdictype:update")
    public String toUpdate(@PathVariable Long cdcDictypeId, Model model) {
        CdcDictypeEntity cdcDictype = this.cdcDictypeService.selectById(cdcDictypeId);
        model.addAttribute("cdcDictype", cdcDictype);
        return PREFIX + "cdcdictype_edit.html";
    }

    /**
     * 获取业务字典类型列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcdictype:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcDictypeEntity> result = null;
        Page<CdcDictypeEntity> page = new PageFactory<CdcDictypeEntity>().defaultPage();
        if ((CommonUtil.isNotEmpty(params.get("NotQuery"))) && (params.get("NotQuery").toString().equals("true"))) {
            result = new ArrayList<CdcDictypeEntity>();
        } else {
            result = this.cdcDictypeService.page(page, params);
        }
        page.setRecords((List<CdcDictypeEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query")
    @Permission("cdcdictype:list")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params) {
        List<CdcDictypeEntity> result = this.cdcDictypeService.list(params);
        return result;
    }

    /**
     * 新增业务字典类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcdictype:add")
    @BussinessLog(value = "新增业务字典类型")
    public Object add(CdcDictypeEntity cdcDictype) {
        if (CommonUtil.isOneEmpty(cdcDictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcDictype.insert();
        return R.ok();
    }

    /**
     * 删除业务字典类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcdictype:delete")
    @BussinessLog(value = "删除业务字典类型")
    public Object delete(@RequestParam Long cdcDictypeId) {

        this.cdcDictypeService.deleteById(cdcDictypeId);

        return R.ok();
    }

    /**
     * 修改业务字典类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcdictype:update")
    @BussinessLog(value = "修改业务字典类型")
    public Object update(CdcDictypeEntity cdcDictype) {
        if (CommonUtil.isOneEmpty(cdcDictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcDictypeEntity old = this.cdcDictypeService.selectById(cdcDictype.getId());

        old.setCode(cdcDictype.getCode());
        old.setDescription(cdcDictype.getDescription());
        old.setDataId(cdcDictype.getDataId());
        old.setIsActive(cdcDictype.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }

}
